import sdk from '../../core';
import { Item } from "../../shared/face/index";
import { BaseBody } from '../../core/rpg';
import { ComType } from '../../core/tool/component';
import { analysisIntent } from '../../core/tool/baseScene';
import bag_component from '../../component/bag_component';
import gameCfg from '../../gameCfg';
import { ButtonIntentConfig } from '../../core/tool/Icard';
import { mathType } from '../../core/tool/face';
import { BaseService } from '../BaseService';
import base_component from '../../component/base_component';
import { BagFuncResult } from './types';

/**
 * 改名功能
 * @param useItem 使用的物品
 * @param useCount 使用数量
 * @param funcData 函数数据
 * @param player 玩家对象
 * @returns 使用结果
 */
export async function reName(useItem: Item, useCount: number, funcData: any, player: BaseBody): Promise<BagFuncResult> {
    return new Promise((resolve) => {
        let pop = player.getComponent(ComType.对话框) as any;
        pop._content = '请艾特我直接输入要修改的名字';
        let com_bag = player.getComponent(ComType.背包) as bag_component;
        pop.lisen((_data: analysisIntent) => {
            let reName = _data.Message.content.replace('改名', '');
            if (!BaseService.isNameAvailable(reName)) {
                pop._content = '该名字已被使用，请重新艾特我直接输入要修改的名字';
                pop.active();
                return;
            }
            let card = sdk.tool.card.create();
            card.title('改名确认')
            card.l(`角色改名为【${reName}】`)

            // com_bag.order_look(gameCfg.changeName, card);
            // 配置化按钮意图 - mathType包含在配置中
            const confirmButtonConfigs: ButtonIntentConfig[] = [
                {
                    text: '确认',
                    mathType: mathType.完全匹配,
                    action: (() => {
                        let baseData = player.getComponent(ComType.base) as base_component;
                        const updateResult = BaseService.updateUserName(player.id, baseData.nickName, reName);
                        if (updateResult.success) {
                            baseData.setName(reName);
                            player.client_text(updateResult.message);
                            pop.close();
                            resolve({ 
                                success: true, 
                                message: '改名成功',
                                data: { newName: reName }
                            });
                        } else {
                            pop.active();
                        }
                    })
                },
                {
                    text: '取消',
                    mathType: mathType.完全匹配,
                    action: (() => {
                        pop.close();
                        player.client_text('改名取消');
                        resolve({ 
                            success: false, 
                            message: '改名取消',
                            data: { cancelled: true }
                        });
                    })
                }
            ];

            // 使用pop_component的封装方法
            pop.registerPopupButtonIntents(confirmButtonConfigs, null, card);

            player.client_text(card.getText());
        }, null)
        pop.active();
    });
} 